<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\View;
use think\exception\ValidateException;

use app\admin\validate\GoodsValidate;

use app\admin\model\{Admin, Goods, Cate, GoodsImg};

class CardController extends BaseController
{
    // 渲染次卡列表页面
    public function card_list()
    {   
        return view();
    }

    // 渲染次卡添加页面
    public function card_add()
    {   
        $adminModel = new Admin();
        $cateModel = new Cate();

        $shopList = $adminModel->getList(['role_id' => 2], 0);
        $cateList = $cateModel->getList(['type' => 3], 0);

        View::assign('shop_list', $shopList['data']);
        View::assign('cate_list', $cateList['data']);
        return view();
    }

    // 渲染次卡编辑页面
    public function card_edit()
    {   
        $data = input('get.');
        $adminModel = new Admin();
        $cateModel  = new Cate();
        $goodsModel = new Goods();
        $goodsImgModel = new GoodsImg();

        $card_info = $goodsModel->getFind(['id' => $data['id']])['data'];

        $shopList = $adminModel->getList(['role_id' => 2], 0);
        $cateList = $cateModel->getList([['type', '=', 3]], 0);

        $card_info['banner_img'] = $goodsImgModel->getList(['goods_id' => $card_info['id'], 'type' => 1, 'goods_type' => 1], 0)['data'];
        $card_info['show_img'] = $goodsImgModel->getList(['goods_id' => $card_info['id'], 'type' => 2, 'goods_type' => 1], 0)['data'];

        View::assign('card', $card_info);
        View::assign('shop_list', $shopList['data']);
        View::assign('cate_list', $cateList['data']);
        return view();
    }

    // 获取次卡列表
    public function card_data()
    {   
        $data = input('get.');

        // 验证数据
        try{
            validate(GoodsValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $goodsModel = new Goods();
        $adminModel = new Admin();

        $where[] = ['type', '=', 3];
        $where[] = ['del_time', '=', 0];

        // 搜索条件
        if($data && isset($data['key'])){
            if(!empty($data['key']['title'])){
                $where[] = ['title', 'like', '%' . $data['key']['title'] . '%'];
            }
        }

        $count = $goodsModel->getNum($where)['data'];

        $field = 'id, admin_id, cate_id, small_img, title, price, status, sell_number, number, use_days, type, create_time';
        $list = $goodsModel->getList($where, 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $v['admin_name'] = $adminModel->getVal(['id' => $v['admin_id']], 'nickname')['data'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    // 次卡添加
    public function card_add_data()
    {   
        $goodsModel = new Goods();
        $goodsImgModel = new GoodsImg();
        $data = input('post.');
        
        $data['small_img'] = request()->file('small_img');

        // 验证数据
        try{
            validate(GoodsValidate::class)->scene('card_add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $goodsImgBanner = json_decode($data['banner_img'], true);
        $goodsImgShow = json_decode($data['show_img'], true);
        unset($data['banner_img']);
        unset($data['show_img']);

        if(empty($goodsImgBanner)){
            $this->error_back('轮播图不能为空');
        }

        $data['type'] = 3;
        $data['small_img'] = fileUp($data['small_img'], 'goods');
        $flag = $goodsModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 轮播图 */
        $goodsImg = [];
        foreach($goodsImgBanner as $key => $val){
            $bannerImgArr = [
                'img'      => $val['url'],
                'type'     => 1,
                'goods_id' => $flag['data'],
                'sort'     => $val['sort'],
            ];
            
            $goodsImg[] = $bannerImgArr;
        }

        /** 详情图 */
        foreach($goodsImgShow as $k => $v){
            $showImgArr = [
                'img'      => $v['url'],
                'type'     => 2,
                'goods_id' => $flag['data'],
                'sort'     => $val['sort'],
            ];
            
            $goodsImg[] = $showImgArr;
        }

        /** 添加图片 */
        $goodsImgModel->addAll($goodsImg);
        $this->success_back('添加成功');
    }

    // 次卡信息修改
    public function card_edit_data()
    {   
        $goodsModel = new Goods();
        $goodsImgModel = new GoodsImg();
        $data = input('post.');
        
        $data['small_img'] = request()->file('small_img');

        if($data['small_img']){
            $field = 'card_edit_img';
        }else{
            $field = 'card_edit';
        }

        // 验证数据
        try{
            validate(GoodsValidate::class)->scene($field)->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $goodsImgBanner = json_decode($data['banner_img'], true);
        $goodsImgShow = json_decode($data['show_img'], true);
        unset($data['banner_img']);
        unset($data['show_img']);

        if(empty($goodsImgBanner)){
            $this->error_back('轮播图不能为空');
        }

        /** 修改商品 */
        if($data['small_img']){
            $data['small_img'] = fileUp($data['small_img'], 'goods');
        }else{
            unset($data['small_img']);
        }

        $flag = $goodsModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 轮播图 */
        $goodsImg = [];
        foreach($goodsImgBanner as $key => $val){
            $bannerImgArr = [
                'img'      => $val['url'],
                'type'     => 1,
                'goods_id' => $data['id'],
                'sort'     => $val['sort'],
            ];
            
            $goodsImg[] = $bannerImgArr;
        }

        /** 详情图 */
        foreach($goodsImgShow as $k => $v){
            $showImgArr = [
                'img'      => $v['url'],
                'type'     => 2,
                'goods_id' => $data['id'],
                'sort'     => $val['sort'],
            ];
            
            $goodsImg[] = $showImgArr;
        }

        $goodsImgModel->del(['goods_id' => $data['id']]);

        /** 添加图片 */
        $goodsImgModel->addAll($goodsImg);
        $this->success_back('编辑成功');
    }

    // 删除次卡
    public function cardDel()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(GoodsValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $goodsModel = new Goods();

        $flag = $goodsModel->edit(['id' => $data['id'], 'del_time' => time()]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
